package org.millioncall.yueyoga.common.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

//created by zangjy on 2011-6-3
//last modified by zangjy on 2011-6-10
public class ZhuwWebAppUtil {
	private static Logger logger = LoggerFactory.getLogger(ZhuwWebAppUtil.class);

	public static boolean isAjaxRequest(HttpServletRequest request) {
		boolean isAjaxRequest = false;
		// 判断是否为ExtJS,JQuery等ajax框架提交，或者手动设置了X-Requested-With的ajax提交
		String requestType = request.getHeader("X-Requested-With");// ExtJS,jQuery等js框架默认都加了些header
		if(requestType != null && requestType.equals("XMLHttpRequest")) {
			return true;
		}
		if(!isAjaxRequest) {
			// 判断是否为手动设置了requestType的ajax提交
			requestType = request.getHeader("requestType");
			if(requestType != null && requestType.equals("ajax")) {
				return true;
			}
		}
		if(!isAjaxRequest) {
			// 根据url传递参数判断是否为ajax提交
			requestType = request.getParameter("requestType");
			if(requestType != null && requestType.equals("ajax")) {
				return true;
			}
		}
		if(!isAjaxRequest) {
			if(request.getServletPath().endsWith(".ajax")) {// servlet名称以.ajax结尾
				return true;
			}
		}
		return false;
	}

	// 解析请求参数
	public static Map<String, String[]> parseQueryString(String s) {
		return ZhuwRequestUtil.parseQueryString(s);
	}

	// ajax 响应
	public static void ajaxResponse(HttpServletResponse response, String json) {
		// ZhuwResponseUtil.renderJson(response, json);//ImageAct调用此方法会出错，何解？？？
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.print(json);
		} catch (IOException e) {
			logger.error("IO异常", e);
		} finally {
			out.flush();
			out.close();
		}
	}

	// 取得客户端ip地址
	public static String getClientIp(HttpServletRequest request) {
		return ZhuwRequestUtil.getIpAddr(request);
	}

	// 判断是否为一个邮箱
	public static boolean isValidEmail(String email) {
		return Pattern.matches("^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$", email);
	}

	// 将10位长字符串时间转换为Date对象
	public static Date convertToDate10(String date) {
		try {
			return new SimpleDateFormat("yyyy-MM-dd").parse(date);
		} catch (ParseException e) {
			logger.error("时间格式转换异常");
			return null;
		}
	}

	// 根据bean名称取得spring中的bean
	public static Object getSpringBean(HttpServletRequest request, String beanName) {
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
		return ctx.getBean(beanName);
	}
}
